package com.alstudio.ui.module.setting;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.module.xmpp.request.ALUiXmppRequestCreator;
import com.alstudio.ui.base.TitleBarActivity;
import com.alstudio.utils.formater.string.StringUtils;
import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.notification.SystemNotificationEnvConfig;
import com.alstudio.utils.notification.SystemSettings;
import com.alstudio.utils.notification.SystemSettings.ModifySettingType;
import com.alstudio.view.dialog.AlertDialogCreator;
import com.alstudio.view.listener.AlDialogOnClickListener;
import com.alstudio.view.tableview.ALBasicListItem;
import com.alstudio.view.tableview.ALTableView;
import com.alstudio.view.tableview.ALTableView.ALTableViewClickListener;
import com.alstudio.view.tableview.ViewItem;
import com.alstudio.view.wheel.widget.OnWheelChangedListener;
import com.alstudio.view.wheel.widget.WheelView;
import com.alstudio.view.wheel.widget.adapters.ArrayWheelAdapter;
import com.loovee.imaohu.R;

/**
 * 通知设定
 * 
 * @author alonso lee
 * 
 */
public class NoticeSettingActivity extends TitleBarActivity implements
		ALTableViewClickListener {

	private ALTableView tableView;

	// 声音开关
	private ALBasicListItem soundItem;
	// 震动开关
	private ALBasicListItem vibrationItem;
	// 来电提醒
	private ALBasicListItem callNoticeItem;
	// 免打扰
	private ALBasicListItem dndItem;

	// 点击声音控制
	private final int SOUNT_ITEM_INDEX = 0;
	// 点击震动开关
	private final int VIBRATION_ITEM_INDEX = 1;
	// 点击来电提醒
	private final int CALL_ITEM_INDEX = 2;
	// 点击免打扰
	private final int DND_ITEM_INDEX = 3;

	private boolean soundState;
	private boolean vbrationState;
	private boolean muteState;
	private boolean callNoticeState;

	// 静音时段
	private String[] hours;

	private String start_time;
	private String end_time;
	private String save_start_time;
	private String save_end_time;

	private boolean isDataChanged;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setupView(this, getClass().getSimpleName());
		initData();
		updateSwitcherState();
	}

	@Override
	protected void setupView(Context context, String name) {
		// TODO Auto-generated method stub
		super.setupView(context, name);
		setTitleBarContentView(R.layout.notice_setting_layout);
		tableView = (ALTableView) findViewById(R.id.aLTableView1);
		hideRightButton(true);
		setCenterTextViewMessage(R.string.TxtNotice);
		initTableView();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		soundState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getSoundState();
		vbrationState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getVbrationState();
		callNoticeState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getCallNoticeState();
		muteState = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getMuteState();
	}

	/**
	 * 初始化列表
	 */
	private void initTableView() {

		tableView.clear();

		// 通知
		soundItem = new ALBasicListItem(getApplicationContext());
		soundItem.setDrawable(R.drawable.icon_system_setting);
		soundItem.title.setText(R.string.TxtSystemSettingSound);
		soundItem.setChevronImg(R.drawable.multi_choice_on);
		ViewItem i = new ViewItem(soundItem);
		tableView.addViewItem(i);

		// 震动
		vibrationItem = new ALBasicListItem(getApplicationContext());
		vibrationItem.setDrawable(R.drawable.icon_system_setting);
		vibrationItem.title.setText(R.string.TxtSystemSettingVibration);
		vibrationItem.setChevronImg(R.drawable.multi_choice_on);
		i = new ViewItem(vibrationItem);
		tableView.addViewItem(i);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 来电提醒
		callNoticeItem = new ALBasicListItem(getApplicationContext());
		callNoticeItem.setDrawable(R.drawable.icon_system_setting);
		callNoticeItem.title.setText(R.string.TxtCallNotice);
		callNoticeItem.setChevronImg(R.drawable.multi_choice_on);
		i = new ViewItem(callNoticeItem);
		tableView.addViewItem(i);

		// 来电提醒说明
		TextView aboutCallNotice = new TextView(getApplicationContext());
		aboutCallNotice.setText(R.string.TxtAboutCallNotice);
		aboutCallNotice.setGravity(Gravity.CENTER);
		i = new ViewItem(aboutCallNotice);
		i.setClickable(false);
		tableView.addViewItem(i);

		// 加入分段
		tableView.addSegmentations(R.layout.default_blank_list_segmentation);

		// 免打扰时段
		dndItem = new ALBasicListItem(getApplicationContext());
		dndItem.setDrawable(R.drawable.icon_system_setting);
		dndItem.title.setText(R.string.TxtDndTitle);
		dndItem.setChevronImg(R.drawable.multi_choice_on);
		i = new ViewItem(dndItem);
		tableView.addViewItem(i);

		// 免打扰说明
		TextView aboutDnd = new TextView(getApplicationContext());
		aboutDnd.setText(R.string.TxtAboutDnd);
		aboutDnd.setGravity(Gravity.CENTER);
		i = new ViewItem(aboutDnd);
		i.setClickable(false);
		tableView.addViewItem(i);

		tableView.setClickListener(this);
		tableView.commit();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.view.tableview.ALTableView.ALTableViewClickListener#onClick
	 * (int)
	 */
	@Override
	public void onClick(int index) {
		// TODO Auto-generated method stub
		isDataChanged = true;
		switch (index) {
		case SOUNT_ITEM_INDEX:
			updateSoundState();
			break;
		case VIBRATION_ITEM_INDEX:
			updateVbrationState();
			break;
		case CALL_ITEM_INDEX:
			updateCallNoticeState();
			break;
		case DND_ITEM_INDEX:
			updateMuteState();
			break;
		}
		updateSwitcherState();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.ui.base.BaseActivity#finish()
	 */
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		if (isDataChanged) {
			// 保存系统设置
			SystemNotificationEnvConfig.getInstance().saveSystemSettings();
		}
	}

	/**
	 * 刷新声音开关状态
	 */
	private void updateSoundState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setSoundState(!soundState);
		soundState = !soundState;
		SystemSettings settings = new SystemSettings();
		settings.setSoundState(soundState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_SOUND);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 刷新振动开关状态
	 */
	private void updateVbrationState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setVbrationState(!vbrationState);
		vbrationState = !vbrationState;
		SystemSettings settings = new SystemSettings();
		settings.setVbrationState(vbrationState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_VIBRATION);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 更新免打扰时段
	 */
	private void updateMuteState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setMuteState(!muteState);
		muteState = !muteState;
		if (muteState) {
			showSetDndTimeDialog();
		}
	}

	/**
	 * 更新来电通知
	 */
	private void updateCallNoticeState() {
		SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.setCallNoticeState(!callNoticeState);
		callNoticeState = !callNoticeState;
		SystemSettings settings = new SystemSettings();
		settings.setCallNoticeState(callNoticeState);
		settings.setModifyType(ModifySettingType.SETTING_MODIFY_TYPE_CALL_NOTICE);
		ALUiXmppRequestCreator.postALUiXmppCommitUserOpt(settings);
	}

	/**
	 * 更新系统设置相关属性的UI
	 */
	private void updateSwitcherState() {
		// 更新声音开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getSoundState()) {
			soundItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			soundItem.setChevronImg(R.drawable.multi_choice_off);
		}

		// 更新振动开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getVbrationState()) {
			vibrationItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			vibrationItem.setChevronImg(R.drawable.multi_choice_off);
		}

		// 更新免打扰开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getMuteState()) {
			dndItem.setChevronImg(R.drawable.multi_choice_on);
			String dnsStr = SystemNotificationEnvConfig.getInstance().getSystemSettings().getMuteStartTime()+"-"+SystemNotificationEnvConfig.getInstance().getSystemSettings().getMuteEndTime();
			dndItem.setCenterMsg(dnsStr);
		} else {
			dndItem.setChevronImg(R.drawable.multi_choice_off);
			dndItem.setCenterMsg("");
		}

		// 更新来电通知开关状态
		if (SystemNotificationEnvConfig.getInstance().getSystemSettings()
				.getCallNoticeState()) {
			callNoticeItem.setChevronImg(R.drawable.multi_choice_on);
		} else {
			callNoticeItem.setChevronImg(R.drawable.multi_choice_off);
		}
	}

	/**
	 * 显示设置静音时段对话框
	 */
	private void showSetDndTimeDialog() {

		hours = getResources().getStringArray(R.array.hours);
		// 获取View
		View view = ALLocalEnv.getLocalEnv().getGlobalLayoutInflater()
				.inflate(R.layout.dnd_time_setting_layout, null);
		WheelView start_time_view = (WheelView) view
				.findViewById(R.id.sys_start_time);
		WheelView end_time_view = (WheelView) view
				.findViewById(R.id.sys_end_time);

		start_time_view.setViewAdapter(new ArrayWheelAdapter<String>(ALLocalEnv
				.getLocalEnv(), hours));
		end_time_view.setViewAdapter(new ArrayWheelAdapter<String>(ALLocalEnv
				.getLocalEnv(), hours));

		String sys_start_time = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getMuteStartTime();
		String sys_end_time = SystemNotificationEnvConfig.getInstance()
				.getSystemSettings().getMuteEndTime();

		int indexY = hours.length / 2;
		int indexM = hours.length / 2;

		if (null != sys_start_time && null != sys_end_time
				&& !"".equals(sys_start_time) && !"".equals(sys_end_time)) {

			indexY = StringUtils.getStringArrayIndex(hours, sys_start_time);
			indexM = StringUtils.getStringArrayIndex(hours, sys_end_time);

		} else if (null != save_start_time && null != save_end_time
				&& !"".equals(save_start_time) && !"".equals(save_end_time)) {

			indexY = StringUtils.getStringArrayIndex(hours, save_start_time);
			indexM = StringUtils.getStringArrayIndex(hours, save_end_time);

		} else {

			indexY = hours.length / 2;
			indexM = hours.length - 1;
		}

		start_time_view.setCurrentItem(indexY);
		end_time_view.setCurrentItem(indexM);

		start_time = hours[indexY];
		end_time = hours[indexM];

		start_time_view.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				start_time = hours[newValue];
			}
		});

		end_time_view.addChangingListener(new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub

				end_time = hours[newValue];
			}
		});

		AlertDialogCreator dialog = AlertDialogCreator
				.createOneButtonWithCustomViewDialog(this, false, view);
		dialog.setCenterKeyListener(new AlDialogOnClickListener() {

			@Override
			public void onClick() {
				// TODO Auto-generated method stub
				if (null != start_time && null != end_time) {

					if (start_time.equals("") && end_time.equals("")) {
						start_time = hours[hours.length / 2];
						end_time = hours[hours.length / 2];
					}

				} else {
					start_time = hours[hours.length / 2];
					end_time = hours[hours.length / 2];
				}

				save_start_time = start_time;
				save_end_time = end_time;

				SystemNotificationEnvConfig.getInstance().getSystemSettings()
						.setMuteStartTime(save_start_time);

				SystemNotificationEnvConfig.getInstance().getSystemSettings()
						.setMuteEndTime(save_end_time);

				ALLog.d("起始时间 "
						+ SystemNotificationEnvConfig.getInstance()
								.getSystemSettings().getMuteStartTime()
						+ " 终止时间 "
						+ SystemNotificationEnvConfig.getInstance()
								.getSystemSettings().getMuteEndTime());

				// AccountManager.saveSystemSettings(App.sysSettings);

			}
		});

		dialog.showDialog();

	}

}
